#pragma GCC optimize(2)
#include <bits/stdc++.h>

using namespace std;
#define int long long
const int inf = 0x3f3f3f3f3f3f3f3f;

void solve()
{
    int n, l1, r1, l2, r2;
    cin >> n >> l1 >> r1 >> l2 >> r2;
    vector<int> a(n + 1);
    for (int i = 3; i <= n; i++) cin >> a[i];
    for (int i = l1; i <= r1 && i <= l1 + 10; i++)
    {
        a[1] = i % 10;
        for (int j = l2; j <= r2 && j <= l2 + 10; j++)
        {
            a[2] = j % 10;
            if (n == 3 && (a[1] * a[2] % 10 == a[3]))
            {
                cout << i << ' ' << j << endl;
                return;
            }
            if ((a[1] * a[2] % 10 == a[3]) && (a[2] * a[3] % 10 == a[4]))
            {
                cout << i << ' ' << j << endl;
                return;
            }
        }
    }
    cout << -1 << ' ' << -1 << endl;
}
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int _ = 1;
    cin >> _;
    while (_--)
    {
        solve();
    }
    return 0;
}